<?php

namespace app\api\controller;
use ba\Tree;
use Throwable;
use think\facade\Db;
use think\facade\Request;
use app\common\controller\Api;
use app\common\library\Upload;
use app\admin\model\RechargeSpace;
use app\admin\model\Space as SpaceModel;
use app\admin\model\Device;
use app\api\validate\Space as SpaceValidate;
class Space extends Api
{   
     /**
     * 新增场地
     * author：mozi
     * time:2025.05.16
     * */
	function sub(){
		$params = $this->request->post("");
		$validate = new SpaceValidate();
        try {
            $validate->check($params);
        } catch (Throwable $e) {
            return json(['code'=>100,'msg'=>$e->getMessage()]);
        }
        $model = new SpaceModel();
        $model->startTrans();
        try {
            if(isset($params['id'])){
                $id = $params['id'];
                unset($params['id']);
                $model->where(['agent_id'=>request()->adminId,'id'=>$id])->update($params);
            }else{
                 //$params['agent_id'] = request()->adminId;
                 $model->save($params);
            }
            $model->commit();
        } catch (Throwable $e) {
            $model->rollback();
            $this->error($e->getMessage());
        }
		return json(['code'=>200,'msg'=>'ok']);
	}
    /**
     * 获取场地列表
     * author：mozi
     * time:2025.05.16
     * */
    function lists(){
          $mode =  $this->request->post("mode",'');
          $page = $this->request->post("page",1);
          $key = $this->request->post("key",'');
          $w = [];
          if($key){
             $w[] = ['name',"like","%".$key."%"];
          }
          $model = new SpaceModel();
          if($mode == 'no_bang'){
                $rid =  $this->request->post("rid",0);
                if($rid == 0){
                     $this->error("rid为空");
                }
                $no_b = [];
                $all_bang_space = RechargeSpace::where("agent_id",request()->adminId)->field("space_id,rid")->select()->toArray();
                foreach($all_bang_space as &$v){
                     if($v['rid'] != $rid){
                        $no_b[] = $v['space_id'];
                     }
                }

                if (!empty($no_b)){
                    $w[] = ['id', 'not in', $no_b];
                }

          }
            
          $res = $model->where("agent_id",request()->adminId)
                    ->where($w)
                    ->order("id desc")
                    ->page($page, 10)
                    ->select();
         $total = $model->where("agent_id",request()->adminId)->count();
         return json(['code'=>200,'msg'=>'ok','data'=>$res??[],'total'=>$total??0]);


    }
    /**
     * 获取场地详细
     * author：mozi
     * time:2025.05.16
     * */
    function detail(){
          $id = $this->request->post("id",0);
          $model = new SpaceModel();
          $res = $model->where("id",$id)->where("agent_id",request()->adminId)->find();
         return json(['code'=>200,'msg'=>'ok','data'=>$res??[]]);


    }
    /**
     * 获取场地所有设备
     * author：mozi
     * time:2025.05.16
     * */
    function devices(){
        $id = $this->request->post("space_id",0);
        if($id){
             $res = Device::where("space_id",$id)->order("id desc")->select();
        }
        return json(['code'=>200,'msg'=>'ok','data'=>$res??[]]);
    }

     /**
     * 附件上传
     * author：mozi
     * time:2025.05.16
     * */
    public function upload(): void
    {
        $file   = $this->request->file('file');
        $driver = $this->request->param('driver', 'local');
        $topic  = $this->request->param('topic', 'default');
         
        try {
            $upload     = new Upload();
            $attachment = $upload
                ->setFile($file)
                ->setDriver($driver)
                ->setTopic($topic)
                ->upload(null, 0, 2);
            unset($attachment['create_time'], $attachment['quote']);
        } catch (Throwable $e) {
            $this->error($e->getMessage());
        }
        $attachment['full_url'] = 'http:'.$attachment['full_url'];
        $this->success(__('File uploaded successfully'), [
            'file' => $attachment ?? []
        ]);
    }

}